package com.xg.controller;

import com.xg.common.constant.CommonStatusEnum;
import com.xg.common.request.UsedCarReq;
import com.xg.common.response.UsedCar;
import com.xg.common.result.ApiResult;
import com.xg.common.util.Func;
import com.xg.common.util.PageUtil;
import com.xg.common.util.PageUtils;
import com.xg.service.CarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 汽车后台接口
 */
@RestController
@RequestMapping("/car")
@Slf4j
public class CarController {

    @Autowired
    private CarService carService;

    @RequestMapping("/list")
    public ApiResult<PageUtils> list(@RequestParam Map<String,Object> params) {
        return ApiResult.success(carService.findCarList(params));
    }


    /**
     * 车辆列表查询
     */
    @RequestMapping("/listNew")
    public ApiResult<PageUtil<UsedCar>> listNew(@RequestParam Map<String,Object> params) {
        return ApiResult.success(carService.findCarListNew(params));
    }


    /**
     * 新增二手车
     */
    @RequestMapping("/save")
    public ApiResult save(@RequestBody UsedCarReq usedCarReq){
        UsedCar usedCar = usedCarReq.getUsedCar();
        log.info("usedCar传的值=",usedCar);
        if (!Func.isEmpty(usedCar)) {
            return carService.saveUsedCar(usedCar);
        }
        return ApiResult.error(CommonStatusEnum.USED_CAR_SAVE_ERROR.getCode(), CommonStatusEnum.USED_CAR_SAVE_ERROR.getMsg());
    }

    /**
     * 二手车表删除结合批量和单个删除
     */
    @RequestMapping("/delete")
    public ApiResult deleteUsedCar(@RequestBody List<Integer> ids) {
        if (!Func.isEmpty(ids)) {
            return carService.deleteUsedCar(ids);
        }
        return ApiResult.error(CommonStatusEnum.USED_CAR_DELETE_ERROR.getCode(), CommonStatusEnum.USED_CAR_DELETE_ERROR.getMsg());
    }

    /**
     * 车系查询单个品牌信息
     */
    @RequestMapping("/info/{usedCarId}")
    public ApiResult<UsedCar> selectUsedCarInfo(@PathVariable("usedCarId") Integer usedCarId) {
        if (!Func.isEmpty(usedCarId)) {
            return carService.selectUsedCarInfo(usedCarId);
        }
        return ApiResult.error(CommonStatusEnum.USED_CAR_INFO_ERROR.getCode(), CommonStatusEnum.USED_CAR_INFO_ERROR.getMsg());
    }

    /**
     * 批量审核车辆信息
     */
    @RequestMapping("/audit")
    public ApiResult auditUsedCar(@RequestBody UsedCarReq usedCarReq) {
        if (!Func.isEmpty(usedCarReq)) {
            return carService.auditUsedCar(usedCarReq);
        }
        return ApiResult.error(CommonStatusEnum.USED_CAR_AUDIT_ERROR.getCode(), CommonStatusEnum.USED_CAR_AUDIT_ERROR.getMsg());
    }
}
